package com.broada.apm.utils;

import java.util.Arrays;
import java.util.Date;

import com.broada.apm.model.ConfigItem;
import com.broada.apm.model.DictMetrics;
import com.broada.apm.model.Dimension.Area;
import com.broada.apm.model.Dimension.Browser;
import com.broada.apm.model.Dimension.Device;
import com.broada.apm.model.Dimension.DimensionSourceType;
import com.broada.apm.model.Domain;
import com.broada.apm.model.ErrorDetail;
import com.broada.apm.model.ErrorRecord;
import com.broada.apm.model.IPLocation;
import com.broada.apm.model.L7ModuelPanel;
import com.broada.apm.model.L7moduleMetric;
import com.broada.apm.model.TransactionRecord;
import com.broada.apm.model.performance.BasePerformance;
import com.broada.apm.threshold.ThresholdRule;
import com.broada.apm.thrift.model.Application;
import com.broada.apm.thrift.model.Relation;
import com.broada.apm.thrift.model.SSLCertificate;
import com.broada.apm.thrift.model.Service;
import com.broada.apm.thrift.model.TransactionChannel;
import com.broada.apm.thrift.model.TransactionDetail;

public class ThriftDataConvertUtils {

	public static Application convertAppForThrift(com.broada.apm.model.ApplicationSystem app) {
		if (app == null) {
			return null;
		}
		Application application = new Application();
		application.setId(app.getId());
		application.setName(app.getName());
		application.setKeyNodeId(app.getKeyNodeId());
		if (app.getRelationIds() != null && app.getRelationIds().length > 0) {
			application.setRelationIds(Arrays.asList(app.getRelationIds()));
		}
		if (app.getServiceIds() != null && app.getServiceIds().length > 0) {
			application.setServiceIds(Arrays.asList(app.getServiceIds()));
		}
		application.setApdex_t(app.getApdex_t());
		application.setCreateAt(app.getCreateAt().getTime());
		application.setDisplayOrder(app.getDisplayOrder());
		application.setBusinessDisplayOrder(app.getBusinessDisplayOrder());
		application.setAreaCode(app.getAreaCode());
		if (app.isOverview() == true) {
			application.setIsOverview(1);
		} else {
			application.setIsOverview(0);
		}
		application.setValidTime(app.getValidTime());
		return application;
	}
	
	public static com.broada.apm.model.ApplicationSystem convertAppForData(Application application) {
		if (application == null) {
			return null;
		}
		com.broada.apm.model.ApplicationSystem app = new com.broada.apm.model.ApplicationSystem();
		app.setId(application.getId());
		app.setName(application.getName());
		app.setKeyNodeId(application.getKeyNodeId());
		if (application.getRelationIds() != null && application.getRelationIds().size() > 0) {
			app.setRelationIds((String[]) application.getRelationIds().toArray(new String[application.getRelationIds().size()]));
		}
		if (application.getRelationIds() != null && application.getRelationIds().size() > 0) {
			app.setServiceIds((String[]) application.getServiceIds().toArray(new String[application.getServiceIds().size()]));
		}
		app.setApdex_t((float) application.getApdex_t());
		app.setCreateAt(new Date(application.getCreateAt()));
		app.setDisplayOrder(application.getDisplayOrder());
		app.setBusinessDisplayOrder(application.getBusinessDisplayOrder());
		app.setAreaCode(application.getAreaCode());
		if (application.getIsOverview() == 1) {
			app.setOverview(true);
		} else {
			app.setOverview(false);
		}
		app.setValidTime(application.getValidTime());
		return app;
	}
	
	public static Service convertServiceForThrift(com.broada.apm.model.Service serv) {
		if(serv == null) return null;
		Service service  = new Service();
		service.setId(serv.getId());
		service.setName(serv.getName());
		service.setIp(serv.getIp());
		service.setIpNum(serv.getIpNum());
		service.setPort(serv.getPort());
		if(serv.getCreatedAt() != null)
			service.setCreatedAt(serv.getCreatedAt().getTime());
		service.setX(serv.getX());
		service.setY(serv.getY());
		service.setSize(serv.getSize());
		service.setProtocol(serv.getProtocol());
		service.setAutoDiscoveryProtocol(serv.getAutoDiscoveryProtocol());
		service.setL4protocol(serv.getL4protocol());
		service.setCreateBy(serv.getCreateBy());
		service.setNodeType(serv.getNodeType());
		if (serv.getNodeIds() != null && serv.getNodeIds().length > 0) {
			service.setNodeIds(Arrays.asList(serv.getNodeIds()));
		}
		if (serv.getFilterIps() != null && serv.getFilterIps().length > 0) {
			service.setFilterIps(Arrays.asList(serv.getFilterIps()));
		}
		service.setTx(serv.getTx());
		service.setRx(serv.getRx());
		service.setLua(serv.getLua());
		service.setFocus(serv.getFocus());
		return service;
	}
	
	public static com.broada.apm.model.Service convertServiceForData(Service serv) {
		if(serv == null) return null;
		com.broada.apm.model.Service service  = new com.broada.apm.model.Service();
		service.setId(serv.getId());
		service.setName(serv.getName());
		service.setIp(serv.getIp());
		service.setIpNum(serv.getIpNum());
		service.setPort(serv.getPort());
		if (serv.getCreatedAt() > 0) {
			service.setCreatedAt(new Date(serv.getCreatedAt()));
		}
		service.setX(serv.getX());
		service.setY(serv.getY());
		service.setSize(serv.getSize());
		service.setProtocol(serv.getProtocol());
		service.setAutoDiscoveryProtocol(serv.getAutoDiscoveryProtocol());
		service.setL4protocol(serv.getL4protocol());
		service.setCreateBy(serv.getCreateBy());
		service.setNodeType(serv.getNodeType());
		if (serv.getNodeIds() != null && serv.getNodeIds().size() > 0) {
			String[] nodeIds = new String[serv.getNodeIds().size()];
			for (int i=0; i<serv.getNodeIds().size(); i++) {
				nodeIds[i] = serv.getNodeIds().get(i);
			}
			service.setNodeIds(nodeIds);
		}
		if (serv.getFilterIps() != null && serv.getFilterIps().size() > 0) {
			String[] sendNodeIds = new String[serv.getFilterIps().size()];
			for (int i=0; i<serv.getFilterIps().size(); i++) {
				sendNodeIds[i] = serv.getFilterIps().get(i);
			}
			service.setFilterIps(sendNodeIds);
		}
		service.setTx(serv.getTx());
		service.setRx(serv.getRx());
		service.setLua(serv.getLua());
		service.setFocus(serv.getFocus());
		return service;
	}
	
	public static SSLCertificate convertSSLforThrift(com.broada.apm.model.SSLCertificate ssl) {
		if(ssl == null) return null;
		SSLCertificate sslCertificate = new SSLCertificate();
		sslCertificate.setPrivateKey(ssl.getPrivateKey().toString());
		sslCertificate.setServiceId(ssl.getServiceId());
		sslCertificate.setSslPwd(ssl.getSslPwd());
		sslCertificate.setSslType(ssl.getSslType());
		return sslCertificate;
	}
	
	public static com.broada.apm.model.SSLCertificate convertSSLforData(SSLCertificate ssl) {
		if(ssl == null) return null;
		com.broada.apm.model.SSLCertificate sslCertificate = new com.broada.apm.model.SSLCertificate();
		sslCertificate.setPrivateKey(ssl.getPrivateKey());
		sslCertificate.setServiceId(ssl.getServiceId());
		sslCertificate.setSslPwd(ssl.getSslPwd());
		sslCertificate.setSslType(ssl.getSslType());
		return sslCertificate;
	}
	
	public static com.broada.apm.model.Relation convertRelationforData(Relation relation) {
		if(relation == null) return null;
		com.broada.apm.model.Relation rel = new com.broada.apm.model.Relation();
		rel.setFrom(relation.getServiceFrom());
		rel.setTo(relation.getServiceTo());
		rel.setId(relation.getId());
		rel.setName(relation.getName());
		return rel;
	}
	
	public static Relation convertRelationforThrift(com.broada.apm.model.Relation relation) {
		if(relation == null) return null;
		Relation rel = new Relation();
		rel.setServiceFrom(relation.getFrom());
		rel.setServiceTo(relation.getTo());
		rel.setId(relation.getId());
		rel.setName(relation.getName());
		return rel;
	}
	
	public static com.broada.apm.model.ProtocolRelation convertProtocalRelationforData(com.broada.apm.thrift.model.ProtocolRelation protocol) {
		if(protocol == null) return null;
		com.broada.apm.model.ProtocolRelation protocolRelation = new com.broada.apm.model.ProtocolRelation();
		if (protocol.getCreatedAt() > 0) {
			protocolRelation.setCreatedAt(new Date(protocol.getCreatedAt()));
		}
		protocolRelation.setId(protocol.getId());
		protocolRelation.setServiceId(protocol.getServiceId());
		protocolRelation.setSrcIp(protocol.getSrcIp());
		return protocolRelation;
	}
	
	public static com.broada.apm.thrift.model.ProtocolRelation convertProtocalRelationforThrift(com.broada.apm.model.ProtocolRelation protocol) {
		if(protocol == null) return null;
		com.broada.apm.thrift.model.ProtocolRelation protocolRelation = new com.broada.apm.thrift.model.ProtocolRelation();
		if (protocol.getCreatedAt() != null) {
			protocolRelation.setCreatedAt(protocol.getCreatedAt().getTime());
		}
		protocolRelation.setId(protocol.getId());
		protocolRelation.setServiceId(protocol.getServiceId());
		protocolRelation.setSrcIp(protocol.getSrcIp());
		return protocolRelation;
	}
	
	public static com.broada.apm.model.Resource convertResourceforData(com.broada.apm.thrift.model.Resource resource) {
		if(resource == null) return null;
		com.broada.apm.model.Resource resou = new com.broada.apm.model.Resource();
		resou.setApdex_t((float) resource.getApdex_t());
		resou.setApplicationId(resource.getApplicationId());
		resou.setBelongTo(resource.getBelongTo());
		if (resource.getCreatedAt() > 0) {
			resou.setCreatedAt(new Date(resource.getCreatedAt()));
		}
		resou.setId(resource.getId());
		resou.setUri(resource.getUri());
		resou.setSoapFunction(resource.getSoapFunction());
		return resou;
	}
	
	public static com.broada.apm.thrift.model.Resource convertResourceforThrift(com.broada.apm.model.Resource resource) {
		if(resource == null) return null;
		com.broada.apm.thrift.model.Resource resou = new com.broada.apm.thrift.model.Resource();
		resou.setApdex_t(resource.getApdex_t());
		resou.setApplicationId(resource.getApplicationId());
		resou.setBelongTo(resource.getBelongTo());
		if (resource.getCreatedAt() != null) {
			resou.setCreatedAt(resource.getCreatedAt().getTime());
		}
		resou.setId(resource.getId());
		resou.setUri(resource.getUri());
		resou.setSoapFunction(resource.getSoapFunction());
		return resou;
	}
	
	public static Area convertAreaforData(com.broada.apm.thrift.model.Area area) {
		if(area == null) return null;
		Area ar = new Area();
		ar.setCode(area.getCode());
		ar.setId(area.getId());
		ar.setName(area.getName());
		ar.setOrder(area.getOrder());
		ar.setType(area.getType());
		if (DimensionSourceType.REQUEST.equals(area.getSource())) {
			ar.setSource(DimensionSourceType.REQUEST);
		}
		if (DimensionSourceType.RESPONSE.equals(area.getSource())) {
			ar.setSource(DimensionSourceType.RESPONSE);
		}
		return ar;
	}
	
	public static com.broada.apm.thrift.model.Area convertAreaforThrift(Area area) {
		if(area == null) return null;
		com.broada.apm.thrift.model.Area ar = new com.broada.apm.thrift.model.Area();
		ar.setCode(area.getCode());
		ar.setId(area.getId());
		ar.setName(area.getName());
		ar.setOrder(area.getOrder());
		ar.setType(area.getType());
		if (DimensionSourceType.REQUEST.equals(area.getSource())) {
			ar.setSource(com.broada.apm.thrift.model.DimensionSourceType.REQUEST);
		}
		if (DimensionSourceType.RESPONSE.equals(area.getSource())) {
			ar.setSource(com.broada.apm.thrift.model.DimensionSourceType.RESPONSE);
		}
		return ar;
	}
	
	public static Browser convertBrowserforData(com.broada.apm.thrift.model.Browser browser) {
		if(browser == null) return null;
		Browser brow = new Browser();
		brow.setCode(browser.getCode());
		brow.setId(browser.getId());
		brow.setName(browser.getName());
		brow.setOrder(browser.getOrder());
		brow.setType(browser.getType());
		if (DimensionSourceType.REQUEST.equals(browser.getSource())) {
			brow.setSource(DimensionSourceType.REQUEST);
		}
		if (DimensionSourceType.RESPONSE.equals(browser.getSource())) {
			brow.setSource(DimensionSourceType.RESPONSE);
		}
		
		return brow;
	}
	
	public static com.broada.apm.thrift.model.Browser convertBrowserforThrift(Browser browser) {
		if(browser == null) return null;
		com.broada.apm.thrift.model.Browser brow = new com.broada.apm.thrift.model.Browser();
		brow.setCode(browser.getCode());
		brow.setId(browser.getId());
		brow.setName(browser.getName());
		brow.setOrder(browser.getOrder());
		brow.setType(browser.getType());
		if (DimensionSourceType.REQUEST.equals(browser.getSource())) {
			brow.setSource(com.broada.apm.thrift.model.DimensionSourceType.REQUEST);
		}
		if (DimensionSourceType.RESPONSE.equals(browser.getSource())) {
			brow.setSource(com.broada.apm.thrift.model.DimensionSourceType.RESPONSE);
		}
		
		return brow;
	}
	
	public static Device convertDeviceforData(com.broada.apm.thrift.model.Device device) {
		if(device == null) return null;
		Device dev = new Device();
		dev.setCode(device.getCode());
		dev.setId(device.getId());
		dev.setName(device.getName());
		dev.setOrder(device.getOrder());
		dev.setType(device.getType());
		if (DimensionSourceType.REQUEST.equals(device.getSource())) {
			dev.setSource(DimensionSourceType.REQUEST);
		}
		if (DimensionSourceType.RESPONSE.equals(device.getSource())) {
			dev.setSource(DimensionSourceType.RESPONSE);
		}
		
		return dev;
	}
	
	public static com.broada.apm.thrift.model.Device convertDeviceforThrift(Device device) {
		if(device == null) return null;
		com.broada.apm.thrift.model.Device dev = new com.broada.apm.thrift.model.Device();
		dev.setCode(device.getCode());
		dev.setId(device.getId());
		dev.setName(device.getName());
		dev.setOrder(device.getOrder());
		dev.setType(device.getType());
		if (DimensionSourceType.REQUEST.equals(device.getSource())) {
			dev.setSource(com.broada.apm.thrift.model.DimensionSourceType.REQUEST);
		}
		if (DimensionSourceType.RESPONSE.equals(device.getSource())) {
			dev.setSource(com.broada.apm.thrift.model.DimensionSourceType.RESPONSE);
		}
		
		return dev;
	}
	
	public static com.broada.apm.model.Dimension.Error convertErrorforData(com.broada.apm.thrift.model.Error error) {
		if(error == null) return null;
		com.broada.apm.model.Dimension.Error err = new com.broada.apm.model.Dimension.Error();
		err.setCode(error.getCode());
		err.setId(error.getId());
		err.setName(error.getName());
		err.setOrder(error.getOrder());
		err.setType(error.getType());
		err.setErrorMessage(error.getErrorMessage());
		err.setProtocol(error.getProtocol());
		err.setStatus(error.getStatus());
		err.setCreateBy(error.getCreateBy());
		if (DimensionSourceType.REQUEST.equals(error.getSource())) {
			err.setSource(DimensionSourceType.REQUEST);
		}
		if (DimensionSourceType.RESPONSE.equals(error.getSource())) {
			err.setSource(DimensionSourceType.RESPONSE);
		}
		
		return err;
	}
	
	public static com.broada.apm.thrift.model.Error convertErrorforThrift(com.broada.apm.model.Dimension.Error error) {
		if(error == null) return null;
		com.broada.apm.thrift.model.Error err = new com.broada.apm.thrift.model.Error();
		err.setCode(error.getCode());
		err.setId(error.getId());
		err.setName(error.getName());
		err.setOrder(error.getOrder());
		err.setType(error.getType());
		err.setErrorMessage(error.getErrorMessage());
		err.setProtocol(error.getProtocol());
		err.setStatus(error.getStatus());
		err.setCreateBy(error.getCreateBy());
		if (DimensionSourceType.REQUEST.equals(error.getSource())) {
			err.setSource(com.broada.apm.thrift.model.DimensionSourceType.REQUEST);
		}
		if (DimensionSourceType.RESPONSE.equals(error.getSource())) {
			err.setSource(com.broada.apm.thrift.model.DimensionSourceType.RESPONSE);
		}
		
		return err;
	}
	
	public static com.broada.apm.model.Dimension.OS convertOSforData(com.broada.apm.thrift.model.OS os) {
		if(os == null) return null;
		com.broada.apm.model.Dimension.OS o = new com.broada.apm.model.Dimension.OS();
		o.setCode(os.getCode());
		o.setId(os.getId());
		o.setName(os.getName());
		o.setOrder(os.getOrder());
		o.setType(os.getType());
		if (DimensionSourceType.REQUEST.equals(os.getSource())) {
			o.setSource(DimensionSourceType.REQUEST);
		}
		if (DimensionSourceType.RESPONSE.equals(os.getSource())) {
			o.setSource(DimensionSourceType.RESPONSE);
		}
		
		return o;
	}
	
	public static com.broada.apm.thrift.model.OS convertErrorforThrift(com.broada.apm.model.Dimension.OS os) {
		if(os == null) return null;
		com.broada.apm.thrift.model.OS o = new com.broada.apm.thrift.model.OS();
		o.setCode(os.getCode());
		o.setId(os.getId());
		o.setName(os.getName());
		o.setOrder(os.getOrder());
		o.setType(os.getType());
		if (DimensionSourceType.REQUEST.equals(os.getSource())) {
			o.setSource(com.broada.apm.thrift.model.DimensionSourceType.REQUEST);
		}
		if (DimensionSourceType.RESPONSE.equals(os.getSource())) {
			o.setSource(com.broada.apm.thrift.model.DimensionSourceType.RESPONSE);
		}
		
		return o;
	}
	
	public static IPLocation convertIPLocationforData(com.broada.apm.thrift.model.IPLocation ipLocation) {
		if(ipLocation == null) return null;
		IPLocation ip = new IPLocation();
		ip.setAreaCode(ipLocation.getAreaCode());
		ip.setEndIp(ipLocation.getEndIp());
		ip.setEndIpNum(ipLocation.getEndIpNum());
		ip.setId(ipLocation.getId());
		ip.setMemo(ipLocation.getMemo());
		ip.setStartIp(ipLocation.getStartIp());
		ip.setStartIpNum(ipLocation.getStartIpNum());
		return ip;
	}
	
	public static com.broada.apm.thrift.model.IPLocation convertIPLocationforThrift(IPLocation ipLocation) {
		if(ipLocation == null) return null;
		com.broada.apm.thrift.model.IPLocation ip = new com.broada.apm.thrift.model.IPLocation();
		ip.setAreaCode(ipLocation.getAreaCode());
		ip.setEndIp(ipLocation.getEndIp());
		ip.setEndIpNum(ipLocation.getEndIpNum());
		ip.setId(ipLocation.getId());
		ip.setMemo(ipLocation.getMemo());
		ip.setStartIp(ipLocation.getStartIp());
		ip.setStartIpNum(ipLocation.getStartIpNum());
		return ip;
	}
	
	public static DictMetrics convertDicForData(com.broada.apm.thrift.model.DictMetrics dictMetrics) {
		if(dictMetrics == null) return null;
		DictMetrics dict = new DictMetrics();
		dict.setAppValid(dictMetrics.isAppValid());
		dict.setChart(dictMetrics.getChart());
		dict.setCode(dictMetrics.getCode());
		dict.setCollectionAlogithm(dictMetrics.getCollectionAlogithm());
		dict.setDomainValid(dictMetrics.isDomainValid());
		dict.setId(dictMetrics.getId());
		dict.setLayer(dictMetrics.getLayer());
		dict.setMain(dictMetrics.getMain());
		dict.setMemo(dictMetrics.getMemo());
		dict.setName(dictMetrics.getName());
		dict.setNodeValid(dictMetrics.isNodeValid());
		dict.setParentId(dictMetrics.getParentId());
		dict.setTransform(dictMetrics.getTransform());
		dict.setTransValid(dictMetrics.isTransValid());
		return dict;
	}
	
	public static com.broada.apm.thrift.model.DictMetrics convertDicForThrift(DictMetrics dictMetrics) {
		if(dictMetrics == null) return null;
		com.broada.apm.thrift.model.DictMetrics dict = new com.broada.apm.thrift.model.DictMetrics();
		dict.setAppValid(dictMetrics.isAppValid());
		dict.setChart(dictMetrics.getChart());
		dict.setCode(dictMetrics.getCode());
		dict.setCollectionAlogithm(dictMetrics.getCollectionAlogithm());
		dict.setDomainValid(dictMetrics.isDomainValid());
		dict.setId(dictMetrics.getId());
		dict.setLayer(dictMetrics.getLayer());
		dict.setMain(dictMetrics.getMain());
		dict.setMemo(dictMetrics.getMemo());
		dict.setName(dictMetrics.getName());
		dict.setNodeValid(dictMetrics.isNodeValid());
		dict.setParentId(dictMetrics.getParentId());
		dict.setTransform(dictMetrics.getTransform());
		dict.setTransValid(dictMetrics.isTransValid());
		return dict;
	}
	
	public static L7ModuelPanel convertPanelforData(com.broada.apm.thrift.model.L7ModulePanel panel) {
		if(panel == null) return null;
		L7ModuelPanel l7Module = new L7ModuelPanel();
		l7Module.setAppValid(panel.isAppValid());
		l7Module.setChart(panel.getChart());
		l7Module.setCode(panel.getCode());
		l7Module.setCollectionAlogithm(panel.getCollectionAlogithm());
		l7Module.setDisplayWay(panel.getDisplayWay());
		l7Module.setDomainValid(panel.isDomainValid());
		l7Module.setId(panel.getId());
		l7Module.setLayer(panel.getLayer());
		l7Module.setMain(panel.getMain());
		l7Module.setMemo(panel.getMemo());
		l7Module.setMetricCaption(panel.getMetricCaption());
		l7Module.setMetricCode(panel.getMetricCode());
		l7Module.setMetricId(panel.getMetricId());
		l7Module.setModuleId(panel.getModuleId());
		l7Module.setName(panel.getName());
		l7Module.setNodeValid(panel.isNodeValid());
		l7Module.setPanelIndex(panel.getPanelIndex());
		l7Module.setParentId(panel.getParentId());
		l7Module.setTransform(panel.getTransform());
		l7Module.setTransValid(panel.isTransValid());
		l7Module.setPanelId(panel.getPanelId());
		return l7Module;
	}
	
	public static com.broada.apm.thrift.model.L7ModulePanel convertPanelforThrift(L7ModuelPanel panel) {
		com.broada.apm.thrift.model.L7ModulePanel l7Module = new com.broada.apm.thrift.model.L7ModulePanel();
		l7Module.setAppValid(panel.isAppValid());
		l7Module.setChart(panel.getChart());
		l7Module.setCode(panel.getCode());
		l7Module.setCollectionAlogithm(panel.getCollectionAlogithm());
		l7Module.setDisplayWay(panel.getDisplayWay());
		l7Module.setDomainValid(panel.isDomainValid());
		l7Module.setId(panel.getId());
		l7Module.setLayer(panel.getLayer());
		l7Module.setMain(panel.getMain());
		l7Module.setMemo(panel.getMemo());
		l7Module.setMetricCaption(panel.getMetricCaption());
		l7Module.setMetricCode(panel.getMetricCode());
		l7Module.setMetricId(panel.getMetricId());
		l7Module.setModuleId(panel.getModuleId());
		l7Module.setName(panel.getName());
		l7Module.setNodeValid(panel.isNodeValid());
		l7Module.setPanelIndex(panel.getPanelIndex());
		l7Module.setParentId(panel.getParentId());
		l7Module.setTransform(panel.getTransform());
		l7Module.setTransValid(panel.isTransValid());
		
		return l7Module;
	}
	
	public static L7moduleMetric convertMetricforData(com.broada.apm.thrift.model.L7moduleMetric l7moduleMetric) {
		if(l7moduleMetric == null) return null;
		L7moduleMetric metric = new L7moduleMetric();
		metric.setAppValid(l7moduleMetric.isAppValid());
		metric.setChart(l7moduleMetric.getChart());
		metric.setCode(l7moduleMetric.getCode());
		metric.setCollectionAlogithm(l7moduleMetric.getCollectionAlogithm());
		metric.setDisplayOrder(l7moduleMetric.getDisplayOrder());
		metric.setDomainValid(l7moduleMetric.isDomainValid());
		metric.setId(l7moduleMetric.getId());
		metric.setLayer(l7moduleMetric.getLayer());
		metric.setMain(l7moduleMetric.getMain());
		metric.setMemo(l7moduleMetric.getMemo());
		metric.setMetricCaption(l7moduleMetric.getMetricCaption());
		metric.setMetricId(l7moduleMetric.getMetricId());
		metric.setModuleId(l7moduleMetric.getModuleId());
		metric.setName(l7moduleMetric.getName());
		metric.setNodeValid(l7moduleMetric.isNodeValid());
		metric.setParentId(l7moduleMetric.getParentId());
		metric.setTransform(l7moduleMetric.getTransform());
		metric.setTransValid(l7moduleMetric.isTransValid());
		return metric;
	}
	
	public static com.broada.apm.thrift.model.L7moduleMetric convertMetricforThrift(L7moduleMetric l7moduleMetric) {
		if(l7moduleMetric == null) return null;
		com.broada.apm.thrift.model.L7moduleMetric metric = new com.broada.apm.thrift.model.L7moduleMetric();
		metric.setAppValid(l7moduleMetric.isAppValid());
		metric.setChart(l7moduleMetric.getChart());
		metric.setCode(l7moduleMetric.getCode());
		metric.setCollectionAlogithm(l7moduleMetric.getCollectionAlogithm());
		metric.setDisplayOrder(l7moduleMetric.getDisplayOrder());
		metric.setDomainValid(l7moduleMetric.isDomainValid());
		metric.setId(l7moduleMetric.getId());
		metric.setLayer(l7moduleMetric.getLayer());
		metric.setMain(l7moduleMetric.getMain());
		metric.setMemo(l7moduleMetric.getMemo());
		metric.setMetricCaption(l7moduleMetric.getMetricCaption());
		metric.setMetricId(l7moduleMetric.getMetricId());
		metric.setModuleId(l7moduleMetric.getModuleId());
		metric.setName(l7moduleMetric.getName());
		metric.setNodeValid(l7moduleMetric.isNodeValid());
		metric.setParentId(l7moduleMetric.getParentId());
		metric.setTransform(l7moduleMetric.getTransform());
		metric.setTransValid(l7moduleMetric.isTransValid());
		return metric;
	}
	
	public static Domain convertDomainforData(com.broada.apm.thrift.model.Domain domain) {
		if(domain == null) return null;
		Domain dom = new Domain();
		if (domain.getCreatedAt() > 0) {
			dom.setCreatedAt(new Date(domain.getCreatedAt()));
		}
		dom.setName(domain.getName());
		dom.setProtocol(domain.getProtocol());
		dom.setServiceId(domain.getServiceId());
		return dom;
	}
	
	public static com.broada.apm.thrift.model.Domain convertDomainforThrift(Domain domain) {
		if(domain == null) return null;
		com.broada.apm.thrift.model.Domain dom = new com.broada.apm.thrift.model.Domain();
		if (domain.getCreatedAt() != null) {
			dom.setCreatedAt(domain.getCreatedAt().getTime());
		}
		dom.setName(domain.getName());
		dom.setProtocol(domain.getProtocol());
		dom.setServiceId(domain.getServiceId());
		return dom;
	
	}
	public static ThresholdRule convertThresholdforData(com.broada.apm.thrift.model.ThresholdRule rule) {
		if(rule == null) return null;
		Double[] thresholds = null;
		Integer[] severity = null;
//		Map<String, String> validTime = new HashMap<String, String>();
		if (rule.getThresholds() != null && rule.getThresholds().size() > 1) {
			thresholds = new Double[rule.getThresholds().size()];
			for (int i=0; i<rule.getThresholds().size(); i++) {
				thresholds[i] = rule.getThresholds().get(i);
			}
		} else if (rule.getThresholds() != null && rule.getThresholds().size() == 1) {
			thresholds = new Double[1];
			thresholds[0] = rule.getThresholds().get(0);
		}
		
		if (rule.getSeverity() != null && rule.getSeverity().size() > 1) {
			severity = new Integer[rule.getSeverity().size()];
			for (int i=0; i<rule.getSeverity().size(); i++) {
				severity[i] = rule.getSeverity().get(i);
			}
		} else if (rule.getSeverity() != null && rule.getSeverity().size() == 1) {
			severity = new Integer[1];
			severity[0] = rule.getSeverity().get(0);
		}
		
//		for (Entry<String, String> entry : rule.getValidTime().entrySet()) {
//			String[] time = null;
//			if (entry.getValue() != null && entry.getValue().size() > 1) {
//				time = new String[entry.getValue().size()];
//				for (int i=0; i<time.length; i++) {
//					time[i] = entry.getValue().get(i);
//				}
//			} else if (entry.getValue() != null && entry.getValue().size() == 1) {
//				time = new String[1];
//				time[0] = entry.getValue().get(0);
//			}
//			validTime.put(entry.getKey(), rule.getValidTime());
//		}
		ThresholdRule threshold = new ThresholdRule(rule.getResId(), rule.getField(), thresholds, severity, rule.getTimes());
		return threshold;
	}
	
	public static com.broada.apm.thrift.model.ThresholdRule convertThresholdforThrift(ThresholdRule rule) {
		if(rule == null) return null;
		com.broada.apm.thrift.model.ThresholdRule thresholdRule = new com.broada.apm.thrift.model.ThresholdRule();
		thresholdRule.setField(rule.getField());
		thresholdRule.setResId(rule.getResId());
		thresholdRule.setTimes(rule.getTimes());
		if (rule.getThresholds() != null && rule.getThresholds().length > 0) {
			thresholdRule.setThresholds(Arrays.asList(rule.getThresholds()));
		}
		if (rule.getSeverity() != null && rule.getSeverity().length > 0) {
			thresholdRule.setSeverity(Arrays.asList(rule.getSeverity()));
		}
//		if (rule.getValidTime() != null && rule.getValidTime().size() > 0) {
//			thresholdRule.setValidTime(rule.getValidTime());
//		}
		return thresholdRule;
	}
	
	public static BasePerformance convertBasePerformanceforData(com.broada.apm.thrift.model.BasePerformance perf) {
		if(perf == null) return null;
		@SuppressWarnings("serial")
		BasePerformance basePerformance = new BasePerformance() {
			
			@Override
			protected void perfupdate() {
				
			}
		};
		basePerformance.setApplicationId(perf.getApplicationId());
		basePerformance.setServiceId(perf.getServiceId());
		basePerformance.setResourceId(perf.getResourceId());
		basePerformance.setKey(perf.isKey());
		return basePerformance;
	}
	
	public static com.broada.apm.thrift.model.BasePerformance convertBasePerformanceforThrift(BasePerformance perf) {
		if(perf == null) return null;
		com.broada.apm.thrift.model.BasePerformance basePerformance = new com.broada.apm.thrift.model.BasePerformance();
		basePerformance.setApplicationId(perf.getApplicationId());
		basePerformance.setKey(perf.isKey());
		basePerformance.setServiceId(perf.getServiceId());
		basePerformance.setResourceId(perf.getResourceId());
		return basePerformance;
	}
	
	public static ErrorRecord convertErrorRecordforData(com.broada.apm.thrift.model.ErrorRecord errorRecord) {
		if(errorRecord == null) return null;
		ErrorRecord er = new ErrorRecord();
		er.setApplicationId(errorRecord.getAppId());
		er.setDimensionId(errorRecord.getDimensionId());
		er.setDimensionType(errorRecord.getDimensionType());
		er.setDomainId(errorRecord.getDomainId());
		er.setId(errorRecord.getId());
		er.setMessage(errorRecord.getMessage());
		er.setProtocol(errorRecord.getProtocol());
		er.setResourceId(errorRecord.getResourceId());
		er.setServiceId(errorRecord.getServiceId());
		er.setSubProtocol(errorRecord.getSubProtocol());
		if (errorRecord.getTimestamp() > 0) {
			er.setTimestamp(new Date(errorRecord.getTimestamp()));
		}
		er.setUri(errorRecord.getUri());
		er.setTmpUri(errorRecord.getTmpUri());
		er.setTmpDimensionId(errorRecord.getTmpDimensionId());
		return er;
	}
	
	public static com.broada.apm.thrift.model.ErrorRecord convertErrorRecordforThrift(ErrorRecord errorRecord) {
		if(errorRecord == null) return null;
		com.broada.apm.thrift.model.ErrorRecord er = new com.broada.apm.thrift.model.ErrorRecord();
		er.setAppId(errorRecord.getApplicationId());
		er.setDimensionId(errorRecord.getDimensionId());
		er.setDimensionType(errorRecord.getDimensionType());
		er.setDomainId(errorRecord.getDomainId());
		er.setId(errorRecord.getId());
		er.setMessage(errorRecord.getMessage());
		er.setProtocol(errorRecord.getProtocol());
		er.setResourceId(errorRecord.getResourceId());
		er.setServiceId(errorRecord.getServiceId());
		er.setSubProtocol(errorRecord.getSubProtocol());
		if (errorRecord.getTimestamp() != null) {
			er.setTimestamp(errorRecord.getTimestamp().getTime());
		}
		er.setUri(errorRecord.getUri());
		er.setTmpUri(errorRecord.getTmpUri());
		er.setTmpDimensionId(errorRecord.getTmpDimensionId());
		return er;
	}
	
	public static ConfigItem convertConfigItemforData(com.broada.apm.thrift.model.ConfigItem configItem) {
		if(configItem == null) return null;
		ConfigItem cf = new ConfigItem();
		cf.setKey(configItem.getKey());
		cf.setValue(configItem.getValue());
		return cf;
	}
	
	public static com.broada.apm.thrift.model.ConfigItem convertConfigItemforThrift(ConfigItem configItem) {
		if(configItem == null) return null;
		com.broada.apm.thrift.model.ConfigItem cf = new com.broada.apm.thrift.model.ConfigItem();
		cf.setKey(configItem.getKey());
		cf.setValue(configItem.getValue());
		return cf;
	}
	
	public static com.broada.apm.model.TransactionChannel convertTransactionChannelforData(TransactionChannel channel) {
		if (channel == null) return null;
		com.broada.apm.model.TransactionChannel transactionChannel = new com.broada.apm.model.TransactionChannel();
		transactionChannel.setChannelCode(channel.getChannelCode());
		transactionChannel.setChannelName(channel.getChannelName());
		transactionChannel.setAppId(channel.getAppId());
		return transactionChannel;
	}
	
	public static TransactionChannel convertTransactionChannelforThrift(com.broada.apm.model.TransactionChannel channel) {
		if (channel == null) return null;
		TransactionChannel transactionChannel = new TransactionChannel();
		transactionChannel.setChannelCode(channel.getChannelCode());
		transactionChannel.setChannelName(channel.getChannelName());
		transactionChannel.setAppId(channel.getAppId());
		return transactionChannel;
	}
	
	public static com.broada.apm.model.TransactionType convertTransactionTypeforData(com.broada.apm.thrift.model.TransactionType type) {
		if (type == null) return null;
		com.broada.apm.model.TransactionType transactionType = new com.broada.apm.model.TransactionType();
		transactionType.setTypeCode(type.getTypeCode());
		transactionType.setTypeName(type.getTypeName());
		transactionType.setAppId(type.getAppId());
		transactionType.setL7SubProto(type.getL7SubProto());
		transactionType.setSubTypeCode(type.getSubTypeCode());
		transactionType.setSubTypeName(type.getSubTypeName());
		return transactionType;
	}
	
	public static com.broada.apm.thrift.model.TransactionType convertTransactionTypeforThrift(com.broada.apm.model.TransactionType type) {
		if (type == null) return null;
		com.broada.apm.thrift.model.TransactionType transactionType = new com.broada.apm.thrift.model.TransactionType();
		transactionType.setTypeCode(type.getTypeCode());
		transactionType.setTypeName(type.getTypeName());
		transactionType.setAppId(type.getAppId());
		transactionType.setL7SubProto(type.getL7SubProto());
		transactionType.setSubTypeCode(type.getSubTypeCode());
		transactionType.setSubTypeName(type.getSubTypeName());
		return transactionType;
	}
	
	public static TransactionRecord convertTransactionRecordforData(
			com.broada.apm.thrift.model.TransactionRecord tr) {
		if (tr == null) return null;
		TransactionRecord transactionRecord = new TransactionRecord();
		transactionRecord.setId(tr.getId());
		transactionRecord.setSrcIp(tr.getSrcIp());
		transactionRecord.setDstIp(tr.getDstIp());
		transactionRecord.setServiceId(tr.getServiceId());
		transactionRecord.setResourceId(tr.getResourceId());
		transactionRecord.setProtocol(tr.getProtocol());
		transactionRecord.setL7SubProto(tr.getL7SubProto());
		transactionRecord.setCode(tr.getCode());
		transactionRecord.setUri(tr.getUri());
		transactionRecord.setName(tr.getName());
		transactionRecord.setChannelName(tr.getChannelName());
		transactionRecord.setChannelCode(tr.getChannelCode());
		transactionRecord.setTypeCode(tr.getTypeCode());
		transactionRecord.setTypeName(tr.getTypeName());
		transactionRecord.setSubTypeCode(tr.getSubTypeCode());
		transactionRecord.setSubTypeName(tr.getSubTypeName());
		if (tr.getStartTime() > 0) {
			transactionRecord.setStartTime(new Date(tr.getStartTime()));
		}
		if (tr.getEndTime() > 0) {
			transactionRecord.setEndTime(new Date(tr.getEndTime()));
		}
		transactionRecord.setResponseCode(tr.getResponseCode());
		transactionRecord.setResponseCodeString(tr.getResponseCodeString());
		transactionRecord.setErrorMessage(tr.getErrorMessage());
		transactionRecord.setResponseTime(tr.getResponseTime());
		transactionRecord.setConnectionSynTime(tr.getConnectionSynTime());
		transactionRecord.setRequestSendTime(tr.getRequestSendTime());
		transactionRecord.setResponseServerTime(tr.getResponseServerTime());
		transactionRecord.setDownloadTime(tr.getDownloadTime());
		transactionRecord.setRx(tr.getRx());
		transactionRecord.setTx(tr.getTx());
		transactionRecord.setIsError(tr.isIsError());
		transactionRecord.setApplicationId(tr.getAppId());
		transactionRecord.setTransactionType(tr.getTransactionType());
		transactionRecord.setStartTimeMillion(tr.getStartTimeMillion());
		transactionRecord.setStability_level1(tr.getStability_level1());
		transactionRecord.setStability_level2(tr.getStability_level2());
		transactionRecord.setScale_level1(tr.getScale_level1());
		transactionRecord.setApdexFlag(tr.getApdexFlag());
		return transactionRecord;
	}

	public static com.broada.apm.thrift.model.TransactionRecord convertTransactionRecordforThrift(
			TransactionRecord tr) {
		if (tr == null) return null;
		com.broada.apm.thrift.model.TransactionRecord transactionRecord = new com.broada.apm.thrift.model.TransactionRecord();
		transactionRecord.setId(tr.getId());
		transactionRecord.setSrcIp(tr.getSrcIp());
		transactionRecord.setDstIp(tr.getDstIp());
		transactionRecord.setServiceId(tr.getServiceId());
		transactionRecord.setResourceId(tr.getResourceId());
		transactionRecord.setProtocol(tr.getProtocol());
		transactionRecord.setL7SubProto(tr.getL7SubProto());
		transactionRecord.setCode(tr.getCode());
		transactionRecord.setUri(tr.getUri());
		transactionRecord.setName(tr.getName());
		transactionRecord.setChannelName(tr.getChannelName());
		transactionRecord.setChannelCode(tr.getChannelCode());
		transactionRecord.setTypeCode(tr.getTypeCode());
		transactionRecord.setTypeName(tr.getTypeName());
		transactionRecord.setSubTypeCode(tr.getSubTypeCode());
		transactionRecord.setSubTypeName(tr.getSubTypeName());
		if (tr.getStartTime() != null) {
			transactionRecord.setStartTime(tr.getStartTime().getTime());
		}
		if (tr.getEndTime() != null) {
			transactionRecord.setEndTime(tr.getEndTime().getTime());
		}
		transactionRecord.setResponseCode(tr.getResponseCode());
		transactionRecord.setResponseCodeString(tr.getResponseCodeString());
		transactionRecord.setErrorMessage(tr.getErrorMessage());
		transactionRecord.setResponseTime(tr.getResponseTime());
		transactionRecord.setConnectionSynTime(tr.getConnectionSynTime());
		transactionRecord.setRequestSendTime(tr.getRequestSendTime());
		transactionRecord.setResponseServerTime(tr.getResponseServerTime());
		transactionRecord.setDownloadTime(tr.getDownloadTime());
		transactionRecord.setRx(tr.getRx());
		transactionRecord.setTx(tr.getTx());
		transactionRecord.setIsError(tr.getIsError());
		transactionRecord.setAppId(tr.getApplicationId());
		transactionRecord.setTransactionType(tr.getTransactionType());
		transactionRecord.setStartTimeMillion(tr.getStartTimeMillion());
		transactionRecord.setStability_level1(tr.getStability_level1());
		transactionRecord.setStability_level2(tr.getStability_level2());
		transactionRecord.setScale_level1(tr.getScale_level1());
		transactionRecord.setApdexFlag(tr.getApdexFlag());
		return transactionRecord;
	}

	public static com.broada.apm.model.TransactionDetail convertTransactionDetailforData(TransactionDetail td) {
		com.broada.apm.model.TransactionDetail transactionDetail = new com.broada.apm.model.TransactionDetail();
		transactionDetail.setTransactionId(td.getTransactionId());
		transactionDetail.setL7Message(td.getL7Message());
		transactionDetail.setApplicationId(td.getApplicationId());
		transactionDetail.setId(td.getId());
		if (td.getStartTime() > 0) {
			transactionDetail.setStartTime(new Date(td.getStartTime()));
		}
		if (td.getEndTime() > 0) {
			transactionDetail.setEndTime(new Date(td.getEndTime()));
		}
		return transactionDetail;
	}
	
	public static TransactionDetail convertTransactionDetailforThrift(com.broada.apm.model.TransactionDetail td) {
		TransactionDetail transactionDetail = new TransactionDetail();
		transactionDetail.setTransactionId(td.getTransactionId());
		transactionDetail.setApplicationId(td.getApplicationId());
		transactionDetail.setL7Message(td.getL7Message());
		transactionDetail.setId(td.getId());
		if (td.getStartTime() != null) {
			transactionDetail.setStartTime(td.getStartTime().getTime());
		}
		if (td.getEndTime() != null) {
			transactionDetail.setEndTime(td.getEndTime().getTime());
		}
		return transactionDetail;
	}
	
	public static com.broada.apm.thrift.model.ErrorDetail convertErrorDetailforThrift(ErrorDetail errorDetail) {
		if(errorDetail == null) return null;
		com.broada.apm.thrift.model.ErrorDetail er = new com.broada.apm.thrift.model.ErrorDetail();
		er.setId(errorDetail.getId());
		er.setErrorId(errorDetail.getErrorId());
		er.setApplicationId(errorDetail.getApplicationId());
		if (errorDetail.getTimestamp() != null) {
			er.setTimestamp(errorDetail.getTimestamp().getTime());
		}
		er.setL7Message(errorDetail.getL7Message());
		return er;
	}
	
	public static ErrorDetail convertErrorDetailforData(com.broada.apm.thrift.model.ErrorDetail errorDetail) {
		if(errorDetail == null) return null;
		ErrorDetail er = new ErrorDetail();
		er.setId(errorDetail.getId());
		er.setErrorId(errorDetail.getErrorId());
		er.setApplicationId(errorDetail.getApplicationId());
		if (errorDetail.getTimestamp() != 0l) {
			er.setTimestamp(new Date(errorDetail.getTimestamp()));
		}
		er.setL7Message(errorDetail.getL7Message());
		return er;
	}
}
